# 1.5单变量数据拟合（最小二乘法）

x = [-1, 0, 1, 2, 3, 4, 5, 6]
y = [10, 9, 7, 5, 4, 3, 0, -1]
n = len(x)
sx = 0
sy = 0
sxx = 0
sxy = 0
for i in range(n):
    sx += x[i]**2
    sy += y[i]
    sxx += x[i] ** 4
    sxy += x[i]**2 * y[i]
print(sx, sy, sxx, sxy)
a = (sxx * sy - sx * sxy) / (n * sxx - sx ** 2)
b = (n * sxy - sx * sy) / (n * sxx - sx ** 2)
print("a=%.4f, b=%.4f" % (a, b))
